package com.zzyl.controller.customer;

import com.aliyun.iot20180120.models.QueryDevicePropertyStatusRequest;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.service.DeviceService;
import com.zzyl.service.MemberService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 用户管理
 */
@Slf4j
@Api(tags = "客户管理")
@RestController
@RequestMapping("/customer/user")
public class CustomerUserController extends BaseController {

    @Autowired
    private MemberService memberService;

    @Autowired
    private DeviceService deviceService;


    /**
     * C端用户登录--微信登录
     *
     * @param userLoginRequestDto 用户登录信息
     * @return 登录结果
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public ResponseResult<LoginVo> login(@RequestBody UserLoginRequestDto userLoginRequestDto) throws IOException {
        LoginVo login = memberService.login(userLoginRequestDto);
        return success(login);
    }

    /**
     * 查询指定设备的物模型运行状态
     *
     * @param request
     * @return
     */
    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation(value = "查询指定设备的物模型运行状态", notes = "查询指定设备的物模型运行状态")
    public ResponseResult queryDevicePropertyStatus(@RequestBody QueryDevicePropertyStatusRequest request) {
        return deviceService.queryDevicePropertyStatus(request);
    }

    /**
     * 按天统计查询指标数据
     *
     * @param iotId
     * @return
     */
    @GetMapping("/queryDeviceDataListByDay")
    @ApiOperation(value = "按天统计查询指标数据")
    public ResponseResult<List<DeviceDataGraphVo>> queryDeviceDataListByDay(
            @ApiParam(value = "物联网设备id") @RequestParam("iotId") String iotId,
            @ApiParam(value = "功能id") @RequestParam("functionId") String functionId,
            @ApiParam(value = "开始时间，传入指定日期的0点对应时间戳") @RequestParam("endTime") Long endTime,
            @ApiParam(value = "结束时间，传入指定日期的0点对应时间戳") @RequestParam("startTime") Long startTime
    ) {
        log.info("按天统计查询指标数据!");
        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceService.queryDeviceDataListByDay(iotId, functionId, endTime, startTime);
        return success(deviceDataGraphVoList);
    }

    /**
     * 按周统计查询指标数据
     *
     * @param iotId
     * @param functionId
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("/queryDeviceDataListByWeek")
    @ApiOperation(value = "按周统计查询指标数据")
    public ResponseResult<DeviceDataGraphVo> queryDeviceDataListByWeek(
            @ApiParam(value = "物联网设备id") @RequestParam("iotId") String iotId,
            @ApiParam(value = "功能id") @RequestParam("functionId") String functionId,
            @ApiParam(value = "开始时间，传入指定日期的0点对应时间戳") @RequestParam("startTime") Long startTime,
            @ApiParam(value = "结束时间，传入指定日期的0点对应时间戳") @RequestParam("endTime") Long endTime) {
        log.info("按周统计查询指标数据!");
        List<DeviceDataGraphVo> deviceDataGraphVoList = deviceService.queryDeviceDataListByWeek(iotId, functionId, startTime, endTime);
        return success(deviceDataGraphVoList);
    }
}
